/************************************************/
/*creator: johan candelaria						*/
/*project name: hello_world1					*/
/*notes: dit programma moet een ledje laten		*/
/*		 blinken : D							*/
/*												*/
/*contact: pinkpanter127@hotmail.com			*/
/*												*/
/*hardware: 	pic 18f4550						*/
/*				sw1 d1							*/
/*				led1= d2						*/
/*												*/
/*												*/
/*												*/
/*												*/
/************************************************/
	/*includes*/

// Include header files for used libraries
#include <p18f4550.h>         
#include <delays.h>  
#include <xlcd.h>
#include <stdio.h>
#include <timers.h>
//#include <usart.h>

// Header file with prototypes for LCD functions in lcd.c
#include "lcd.h"


	/*defines*/
#define Fcy  48000000	/* we draaien op ((20mhz/5)*24(=96))/2=48mhz */
	
	/* Set up the configuration bits */
	/*
	DDR == TRIS
	PORT == PORT als je leest, LAT als je schrijft
	#pragma config PWRT = OFF
	#pragma config BOR = OFF
	#pragma config WDT = OFF
	#pragma config CCP2MUX = OFF
	#pragma config LVP = OFF*/
	#pragma config WDT = OFF, BOR = OFF, MCLRE = ON, PWRT = ON, LVP = OFF , CCP2MX = OFF	//gaat goed
	#pragma config PLLDIV = 5, CPUDIV = OSC1_PLL2, USBDIV = 2, FOSC = HSPLL_HS//cpu=hs, ppl enabled+ppl=/4+postscaler=/2. freq=48mhz
	//#pragma config FOSC = HS

	int i, flippie= 0;
	int brghc;
	char TempStr[16]; 
	unsigned char usartbuff[16];


	void longdelay(void){
		for (i=1; i<=4;i++){
		Delay10KTCYx(250);	// delay 10*250 cycles
		}
	}
	
//	void init_rs232(unsigned int snelheid){
//		/* Open the USART configured as 8N1, 9600 baud, in polled mode */
  //			brghc= (((48000000/115200)/16)-1);	//57600
	//		OpenUSART( USART_TX_INT_OFF &
//			USART_RX_INT_OFF &
//			USART_ASYNCH_MODE &
//			USART_EIGHT_BIT &
//			USART_CONT_RX &
//			USART_BRGH_HIGH,
//			brghc );
//			}


void main (void)
	{	ADCON0 = 0x00; // Disable the AD converter
		ADCON1 = 0x0F; // Set all ports to digital
		TRISA = 0x00;	/* configure PORTD for output */
		TRISB = 0b00000001; //rb0=input
		TRISC = 0x00;
		TRISD = 0b00000010;	//rd1=input
		LATDbits.LATD2 = 0; //ledje is active low
		//init_rs232(115200);
		LcdOpen(); // See lcd.c
		/* Open the USART configured as 8N1, 19200 baud, in polled mode */
  			 
			// TXSTA|= 	0b00000100;					//high baudrate
			// baudUSART(BAUD_16_BIT_RATE);


		while(1){

						if(PORTEbits.RE2==1){
							LcdClear();					// Clear display
							LcdSetLine1();				// Put cursor on start of line 1
							printf("LCD Clock DEMO");	// Display text
							//putrsUSART ("U");
							LATD ^= (1 << 2);	
							longdelay();
						}
							
		}// close while(1)
		
		//Delay1KTCYx(10);	// delay 1*10 cycles
		
	}